<template>
  <div class="evaluation">
    <div class="eav-file-box">
      <div>评标附件</div>
      <div class="eva-style">
        <el-descriptions class="margin-top" :column="1" border>
          <el-descriptions-item>
            <template slot="label">专家签到表</template>
            <div style="width: 300px">
              <expertUpload
                :syncedMessage.sync="expertFileInfo"
                ref="expertFile"
              ></expertUpload>
            </div>
          </el-descriptions-item>
          <el-descriptions-item>
            <template slot="label">审查表</template>
            <div style="width: 300px">
              <examUpload
                :syncedMessage.sync="examFileInfo"
                ref="examFile"
              ></examUpload>
            </div>
          </el-descriptions-item>
          <el-descriptions-item>
            <template slot="label">评审表</template>
            <div style="width: 300px">
              <reviewUpload
                :syncedMessage.sync="reviewFileInfo"
                ref="reviewFile"
              ></reviewUpload>
            </div>
          </el-descriptions-item>
        </el-descriptions>
      </div>
      <el-button
        type="primary"
        style="margin-left: 1150px; margin-bottom: 30px; margin-top: 30px"
        @click="saveEva"
        v-show="openMessage.status < 6"
        >提 交</el-button
      >
    </div>
    <div class="eav-box">
      <div>中标候选人</div>
      <div class="eva-style">
        <div class="app-container">
          <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
              <el-button
                type="primary"
                plain
                icon="el-icon-plus"
                size="mini"
                @click="handleAdd"
                v-hasPermi="['purs:cdd:add']"
                v-show="openMessage.status < 6"
                >新增</el-button
              >
              <el-button
                plain
                icon="el-icon-refresh"
                size="mini"
                @click="reload"
                >重 置</el-button
              >
            </el-col>
          </el-row>

          <el-table :data="cddList" border>
            <el-table-column type="selection" width="55" align="center" />
            <!-- 添加序号列 -->
            <el-table-column
              label="序号"
              type="index"
              width="70"
              align="center"
              index-method="indexMethod"
            />

            <el-table-column
              label="供应商名称名称"
              align="center"
              prop="supName"
              width="240px"
            />

            <el-table-column
              label="最终报价(万元)"
              align="center"
              prop="finalQuote"
            />
            <el-table-column
              label="最终得分"
              align="center"
              prop="finalScore"
            />
            <el-table-column label="是否推荐" align="center" prop="recommended">
              <template slot-scope="scope">
                <dict-tag
                  class="dict-box"
                  :options="dict.type.pur_recom_state"
                  :value="scope.row.recommended"
                />
              </template>
            </el-table-column>
            <el-table-column label="排名" align="center" prop="ranking" />

            <el-table-column
              label="操作"
              align="center"
              class-name="small-padding fixed-width"
            >
              <template slot-scope="scope">
                <el-button
                  size="mini"
                  type="text"
                  icon="el-icon-edit"
                  @click="handleUpdate(scope.row)"
                  v-hasPermi="['purs:cdd:edit']"
                  :disabled="bindStatus"
                  >修改</el-button
                >
                <el-button
                  size="mini"
                  type="text"
                  icon="el-icon-delete"
                  @click="handleDelete(scope.row)"
                  :disabled="bindStatus"
                  v-hasPermi="['purs:cdd:remove']"
                  >删除</el-button
                >
              </template>
            </el-table-column>
          </el-table>

          <pagination
            v-show="total > 0"
            :total="total"
            :page.sync="queryParams.pageNum"
            :limit.sync="queryParams.pageSize"
            @pagination="getList"
          />

          <!-- 添加或修改候选人列表对话框 -->
          <el-dialog
            :title="title"
            :visible.sync="open"
            width="500px"
            append-to-body
          >
            <el-form
              ref="form"
              :model="form"
              :rules="rules"
              label-width="100px"
            >
              <el-form-item label="选择候选人" prop="supCode">
                <el-button type="primary" size="mini" @click="openSupList"
                  >投标人列表</el-button
                >
              </el-form-item>
              <el-form-item label="供应商名称" prop="supName">
                <el-input
                  v-model="form.supName"
                  placeholder="请输入供应商名称"
                  readonly
                />
              </el-form-item>
              <el-form-item label="最终报价" prop="finalQuote">
                <el-input
                  v-model="form.finalQuote"
                  placeholder="请输入最终报价"
                />
              </el-form-item>
              <el-form-item label="最终得分" prop="finalScore">
                <el-input
                  v-model="form.finalScore"
                  placeholder="请输入最终得分"
                />
              </el-form-item>
              <el-form-item label="是否推荐" prop="recommended">
                <el-select v-model="recom" clearable placeholder="请选择">
                  <el-option
                    v-for="dict in dict.type.pur_recom_state"
                    :key="dict.value"
                    :label="dict.label"
                    :value="dict.value"
                  />
                </el-select>
              </el-form-item>
              <el-form-item label="排名" prop="ranking">
                <el-input v-model="form.ranking" placeholder="请输入排名" />
              </el-form-item>
              <el-form-item label="是否中标" prop="winningBid">
                <el-select
                  v-model="state"
                  clearable
                  placeholder="请选择"
                  disabled
                >
                  <el-option
                    v-for="dict in dict.type.pur_eav_statu"
                    :key="dict.value"
                    :label="dict.label"
                    :value="dict.value"
                  />
                </el-select>
              </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
              <el-button type="primary" @click="submitForm">确 定</el-button>
              <el-button @click="cancel">取 消</el-button>
            </div>
          </el-dialog>
        </div>
      </div>
    </div>

    <!--投标列表-->
    <el-dialog title="提示" :visible.sync="supDialogVisible" width="60%">
      <div>
        <el-table :data="delvList" border @row-dblclick="handleRowDoubleClick">
          <el-table-column label="供应商编号" align="center" prop="supCode" />
          <el-table-column
            label="供应商名称"
            align="center"
            prop="supName"
            width="240px"
          />
          <el-table-column label="投标状态" align="center" prop="delvStatus">
            <template slot-scope="scope">
              {{ scope.row.delvStatus == 1 ? "投递成功" : "投递失败" }}
            </template>
          </el-table-column>
          <el-table-column
            label="递交时间"
            align="center"
            prop="delvCreateTime"
            width="180"
          >
            <template slot-scope="scope">
              <span>{{
                parseTime(scope.row.delvCreateTime, "{y}-{m}-{d}")
              }}</span>
            </template>
          </el-table-column>

          <el-table-column
            label="操作"
            align="center"
            class-name="small-padding fixed-width"
          >
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="text"
                icon="el-icon-edit"
                @click="uploadFile(scope.row)"
                v-hasPermi="['purs:delv:edit']"
                >下载</el-button
              >
            </template>
          </el-table-column>
        </el-table>

        <pagination
          v-show="total > 0"
          :total="total"
          :page.sync="queryDelvParams.pageNum"
          :limit.sync="queryDelvParams.pageSize"
          @pagination="getDelvList"
        />
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="supDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="supDialogVisible = false"
          >确 定</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>

<script>
import expertUpload from "./expertUpload.vue";
import examUpload from "./examUpload.vue";
import { updateBind } from "@/api/purs/bind";
import reviewUpload from "./reviewUpload.vue";
import { listCdd, getCdd, delCdd, addCdd, updateCdd } from "@/api/purs/cdd";
import { listDelv } from "@/api/purs/delv";
import { getPurCode, deleteFile } from "@/api/purs/purs";
import {
  listEvaFile,
  getEvaFile,
  delEvaFile,
  addEvaFile,
  updateEvaFile,
} from "@/api/purs/evaFile";
export default {
  props: {
    openMessage: {
      type: Object,
    },
  },
  components: {
    expertUpload: expertUpload,
    examUpload: examUpload,
    reviewUpload: reviewUpload,
  },
  dicts: ["pur_eav_statu", "pur_recom_state"],
  data() {
    return {
      expertFileInfo: { id: 1 },
      examFileInfo: { id: 1 },
      reviewFileInfo: { id: 1 },
      supDialogVisible: false,
      queryDelvParams: {
        pageNum: 1,
        pageSize: 10,
      },
      evaFile: {},
      // 遮罩层
      // 总条数
      total: 0,
      // 候选人列表表格数据
      cddList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        supName: [
          { required: true, message: "供应商  名称不能为空", trigger: "blur" },
        ],
        supCode: [
          { required: true, message: "供应商编号不能为空", trigger: "blur" },
        ],
        finalQuote: [
          { required: true, message: "最终报价不能为空", trigger: "blur" },
        ],
        finalScore: [
          { required: true, message: "最终得分不能为空", trigger: "blur" },
        ],
  
        ranking: [{ required: true, message: "排名不能为空", trigger: "blur" }],
      
      },
      state: "1",
      delvList: [],
      recom: "2",
      evaList: [],
      evaCode: "",
      bindStatus: false,
    };
  },
  watch: {
    expertFileInfo: {
      handler(value) {
        if (value.code == 1) {
          this.evaFile.expertFileName = value.fileName;
          this.evaFile.expertFilePath = value.filePath;
        } else if (value.code == 2) {
          //删除文件
          deleteFile(this.evaFile.expertFileName);
          this.evaFile.expertFileName = 0;
          this.evaFile.expertFilePath = 0;
          if (this.evaCode) {
            this.evaFile.evaCode = this.evaCode;
          }
          updateEvaFile(this.evaFile);
        }
      },
    },
    examFileInfo: {
      handler(value) {
        if (value.code == 1) {
          this.evaFile.examFileName = value.fileName;
          this.evaFile.examFilePath = value.filePath;
        } else if (value.code == 2) {
          this.evaFile.examFileName = 0;
          this.evaFile.examFilePath = 0;
          if (this.evaCode) {
            this.evaFile.evaCode = this.evaCode;
          }
          updateEvaFile(this.evaFile);
          //删除文件
          deleteFile(this.evaFile.examileFileName).then();
        } else {
          deleteFile(this.evaFile.examileFileName).then();
        }
      },
    },
    reviewFileInfo: {
      handler(value) {
        if (value.code == 1) {
          this.evaFile.reviewFileName = value.fileName;
          this.evaFile.reviewFilePath = value.filePath;
        } else if (value.code == 2) {
          //删除文件
          deleteFile(this.evaFile.reviewFileName).then();
          this.evaFile.reviewFileName = 0;
          this.evaFile.reviewFilePath = 0;
          if (this.evaCode) {
            this.evaFile.evaCode = this.evaCode;
          }
          updateEvaFile(this.evaFile);
        } else {
          deleteFile(this.evaFile.reviewFileName).then();
        }
      },
    },
  },
  created() {
    this.expertFileInfo.id = 1;
    this.form.bindCode = this.openMessage.bindCode;
    this.form.winningBid = 1;
    this.getCode();
  },
  mounted() {
    if (this.openMessage.status > 5) {
      this.bindStatus = true;
    } else {
      this.bindStatus = false;
    }
    this.getList();
    this.getDelvList();
    this.getEvaList();
  },
  methods: {
    reload() {
      this.getDelvList();
    },

    uploadFile(row) {
      console.log(JSON.stringify(row));
      window.location.href = row.filePath;
    },

    getEvaList() {
      listEvaFile({ bindCode: this.openMessage.bindCode }).then((res) => {
        this.evaCode = res.rows[0].evaCode;
        this.evaFile.expertFileName = res.rows[0].expertFileName;
        this.evaFile.expertFilePath = res.rows[0].expertFilePath;
        this.evaFile.examFilePath = res.rows[0].examFilePath;
        this.evaFile.examFileName = res.rows[0].examFileName;
        this.evaFile.reviewFilePath = res.rows[0].reviewFilePath;
        this.evaFile.reviewFileName = res.rows[0].reviewFileName;
        if (res.rows[0].expertFilePath != 0) {
          let expertFile = [];
          expertFile.push({
            url: res.rows[0].expertFilePath,
            name: res.rows[0].expertFileName,
          });
          this.$refs.expertFile.test(expertFile);
        }

        if (res.rows[0].examFilePath != 0) {
          let examFile = [];
          examFile.push({
            url: res.rows[0].examFilePath,
            name: res.rows[0].examFileName,
          });
          this.$refs.examFile.test(examFile);
        }

        if (res.rows[0].reviewFilePath != 0) {
          let reviewFile = [];
          reviewFile.push({
            url: res.rows[0].reviewFilePath,
            name: res.rows[0].reviewFileName,
          });
          this.$refs.reviewFile.test(reviewFile);
        }
      });
    },

    getCode() {
      let tableName = "pur_eva_file";
      getPurCode(tableName).then((response) => {
        this.evaFile.evaCode = response.data.encodeNow;
      });
    },

    saveEva() {
      if (
        this.evaFile.reviewFilePath == null ||
        this.evaFile.reviewFilePath == 0 ||
        this.evaFile.expertFilePath == null ||
        this.evaFile.expertFilePath == 0 ||
        this.evaFile.examFilePath == null ||
        this.evaFile.examFilePath == 0
      ) {
        this.$modal.msgError("信息填充不完整");
      } else {
        this.evaFile.bindCode = this.openMessage.bindCode;
        addEvaFile(this.evaFile).then((res) => {
          if (res.code == 200) {
            this.$modal.msgSuccess("文件上传成功");
          }
        });
      }
    },

    handleRowDoubleClick(row, event) {
      const index = this.cddList.findIndex(
        (cdd) => cdd.supCode === row.supCode
      );
      if (index !== -1) {
        this.$modal.msgError("请勿重复选取中标候选人");
      } else {
        this.form.supCode = row.supCode;
        this.form.supName = row.supName;
        this.supDialogVisible = false;
      }
    },
    /** 查询投递标书列表 */
    getDelvList() {
      this.queryDelvParams.bindCode = this.openMessage.bindCode;
      listDelv(this.queryDelvParams).then((response) => {
        this.delvList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    openSupList() {
      this.supDialogVisible = true;
    },

    indexMethod(index) {
      // 根据需要自定义序号生成方式，这里只是简单返回索引+1
      return index + 1;
    },
    /** 查询候选人列表列表 */
    getList() {
      this.loading = true;
      this.queryParams.bindCode = this.openMessage.bindCode;
      listCdd(this.queryParams).then((response) => {
        this.cddList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        cddId: null,
        supName: null,
        supCode: null,
        finalQuote: null,
        finalScore: null,
        recommended: null,
        ranking: null,
        winningBid: null,
        bindCode: null,
      };
      this.resetForm("form");
    },

    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加候选人列表";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const cddId = row.cddId || this.ids;
      getCdd(cddId).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改候选人列表";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        this.form.bindCode = this.openMessage.bindCode;
        this.form.recommended = this.recom;
        this.form.winningBid = this.state;
        if (valid) {
          if (this.form.cddId != null) {
            updateCdd(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addCdd(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.getList();
              this.open = false;
              let bind = {
                bindCode: this.openMessage.bindCode,
                status: 5,
              };

              updateBind(bind).then();
              this.open = false;
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const cddIds = row.cddId || this.ids;
      this.$modal
        .confirm('是否确认删除候选人列表编号为"' + cddIds + '"的数据项？')
        .then(function () {
          return delCdd(cddIds);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
  },
};
</script>

<style scope>
.eav-box {
  width: 1300px;
  border: 2px solid #ccc;
  background-color: #ffffff;
  margin: 40px auto;
  border-radius: 18px;
  overflow: auto;
}

.eav-file-box {
  width: 1300px;
  border: 2px solid #ccc;
  background-color: #ffffff;
  margin: 20px auto;
  border-radius: 18px;
  overflow: auto;
}

.eav-box > div:nth-of-type(1) {
  width: 100px;
  height: 40px;
  line-height: 40px;
  text-indent: 20px;
  font-weight: bold;
  color: black;
}
.eva-style {
  width: 1200px;
  margin: 0px auto;
}

.eav-file-box > div:nth-of-type(1) {
  width: 100px;
  height: 40px;
  line-height: 40px;
  text-indent: 20px;
  font-weight: bold;
  color: black;
}
.eva-file-style {
  width: 1200px;

  border: 1px solid black;
  margin: 30px auto;
}
</style>